/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PesquisaSala.java
 *
 * Created on 21/06/2011, 16:54:14
 */

package Formularios;
import DAO.*;
import Logica.*;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pedro H
 */
public class PesquisaSala extends javax.swing.JFrame {

     List<Sala> salas;
    ListSelectionModel lsmSala;
    DefaultTableModel tmSala = new DefaultTableModel(null, new String[]{"id", "tipo", "predio", "andar", "Num.Lugares", "situação"});
    int idsala;

    /** Creates new form PesquisaSala */
    public PesquisaSala() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jCSituacao = new javax.swing.JComboBox();
        jCTipo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jBTodasSalas = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTPesquisaID = new javax.swing.JTextField();
        jBPesquisar = new javax.swing.JButton();
        jBVoltar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTTabela = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(153, 255, 153));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selecione um tipo de Pesquisa: ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel1.setText("Pesquisar por Situação : ");

        jCSituacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Salas Disponíveis", "Salas Alugadas" }));
        jCSituacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCSituacaoActionPerformed(evt);
            }
        });

        jCTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sala de aula", "Auditório", "Sala de reunião" }));
        jCTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCTipoActionPerformed(evt);
            }
        });

        jLabel2.setText("Pesquisar por tipo: ");

        jBTodasSalas.setText("Mostrar todas Salas!");
        jBTodasSalas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTodasSalasActionPerformed(evt);
            }
        });

        jLabel3.setText("Pesquisar por ID: ");

        jBPesquisar.setText("Pesquisar");
        jBPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisarActionPerformed(evt);
            }
        });

        jBVoltar.setText("Voltar");
        jBVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCSituacao, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTPesquisaID, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBPesquisar)))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jBTodasSalas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(jBVoltar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCTipo, 0, 130, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jCSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jCTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBPesquisar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBTodasSalas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jTPesquisaID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jBVoltar)))
                .addContainerGap())
        );

        jTTabela.setModel(tmSala);
        jTTabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmSala = jTTabela.getSelectionModel();
        lsmSala.addListSelectionListener(new ListSelectionListener() { 
            public void valueChanged(ListSelectionEvent e) { 
                if (! e.getValueIsAdjusting()){ 
                    jTTabelaLinhaSelecionada(jTTabela); 
                } 
            }
        });
        jScrollPane1.setViewportView(jTTabela);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCSituacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCSituacaoActionPerformed

        if(jCSituacao.getSelectedItem().equals("Salas Disponíveis")){
            try {

                ListarSalasDiponiveis();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "problemas no campo pesquisa" + ex);
            }
        }
        if(jCSituacao.getSelectedItem().equals("Salas Alugadas")){
            try {
                ListarSalasAlugadas();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "problemas no campo pesquisa" + ex);
            }
        }
}//GEN-LAST:event_jCSituacaoActionPerformed

    private void jCTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCTipoActionPerformed

        try {

            ListarSalaSituacao();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no campo pesquisa" + ex);
        }

    }//GEN-LAST:event_jCTipoActionPerformed

    private void jBTodasSalasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTodasSalasActionPerformed
        try {

            ListarTodasSalas();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no campo pesquisa" + ex);
        }
}//GEN-LAST:event_jBTodasSalasActionPerformed

    private void jBPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisarActionPerformed
        try {

            ListarSalaID();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "problemas no campo pesquisa" + ex);
        }
}//GEN-LAST:event_jBPesquisarActionPerformed

    private void jBVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBVoltarActionPerformed
        dispose();
        
    }//GEN-LAST:event_jBVoltarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PesquisaSala().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBPesquisar;
    private javax.swing.JButton jBTodasSalas;
    private javax.swing.JButton jBVoltar;
    private javax.swing.JComboBox jCSituacao;
    private javax.swing.JComboBox jCTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTPesquisaID;
    private javax.swing.JTable jTTabela;
    // End of variables declaration//GEN-END:variables

       public void ListarSalaID() throws SQLException {

        if (jTPesquisaID.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "digite um ID de uma Sala para pesquisar");
        } else {
            PesquisaDAO dao = new PesquisaDAO();
            salas = dao.getListaIDS(Integer.parseInt(jTPesquisaID.getText()));
            mostraPesquisa(salas);
        }
    }

    public void ListarTodasSalas() throws SQLException {

        PesquisaDAO dao = new PesquisaDAO();
        salas = dao.getListaTodasSalas();
        mostraPesquisa(salas);
    }

    public void ListarSalasDiponiveis() throws SQLException {
        PesquisaDAO dao = new PesquisaDAO();
        salas = dao.getListaSalaDisponivel();
        mostraPesquisa(salas);

    }
     public void ListarSalasAlugadas() throws SQLException {
        PesquisaDAO dao = new PesquisaDAO();
        salas = dao.getListaSalaAlugada();
        mostraPesquisa(salas);

    }

    public void ListarSalaSituacao() throws SQLException{
//Sala de aula, Auditório, Sala de reunião
        if(jCTipo.getSelectedItem().equals("Sala de aula")){
            PesquisaDAO dao = new PesquisaDAO();
            salas = dao.getListaSalaAula();
            mostraPesquisa(salas);
        }
        if(jCTipo.getSelectedItem().equals("Auditório")){
            PesquisaDAO dao = new PesquisaDAO();
            salas = dao.getListaSalaAud();
            mostraPesquisa(salas);
        }
        if(jCTipo.getSelectedItem().equals("Sala de reunião")){
            PesquisaDAO dao = new PesquisaDAO();
            salas = dao.getListaSalaReuniao();
            mostraPesquisa(salas);
        }
        
    }

    private void mostraPesquisa(List<Sala> salas) {

        while (tmSala.getRowCount() > 0) { //vai pegar todas as linha selecionadas maiores que zero, é vai remove-las!
            tmSala.removeRow(0);
        }
        if (salas.isEmpty()) { //se contatos for vazio, ou seja, naum haver nehum cadastrado...
            JOptionPane.showMessageDialog(null, "nehuma Sala cadastrada");
        } else {
            String[] linha = new String[]{null, null, null, null, null, null }; //colocando null em todas as colunas
            for (int i = 0; i < salas.size(); i++) {
                tmSala.addRow(linha);
                tmSala.setValueAt(salas.get(i).getId(), i, 0);
                tmSala.setValueAt(salas.get(i).getTipo(), i, 1);
                tmSala.setValueAt(salas.get(i).getPredio(), i, 2);
                tmSala.setValueAt(salas.get(i).getAndar(), i, 3);
                tmSala.setValueAt(salas.get(i).getNumLugares(), i, 4);
                tmSala.setValueAt(salas.get(i).getSituacao(), i, 5);
            }
        }
    }
   private void jTTabelaLinhaSelecionada(JTable tabela) {

        if (jTTabela.getSelectedRow() != -1) { //"-1" é o cod do erro quando se da quando exclui um contato

            idsala = Integer.parseInt(jTTabela.getValueAt(jTTabela.getSelectedRow(), 0).toString());
            dispose();
            new CadastroReserva(idsala,0).show();


            
        }

    }
}

